package com.sci99.ex.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.sci99.ex.model.Employee;
import com.sci99.ex.model.Note;
import com.sci99.ex.service.CourseAssignService;
import com.sci99.ex.service.NoteService;

public class Opt extends HttpServlet {
	private static final long serialVersionUID = 1L;
	static Logger log = Logger.getLogger(Opt.class);

	public void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String act = StringUtils.defaultString(req.getParameter("act"));
		resp.setContentType("application/json;charset=UTF-8");
		switch (act) {
		case "addCourse":
			addCourse(req, resp);
			break;
		case "removeCourse":
			removeCourse(req, resp);
			break;
		case "saveNote":
			saveNote(req, resp);
			break;
		case "saveNoteFinal":
			saveNoteFinal(req, resp);
			break;
		default:
			Authentication auth = SecurityContextHolder.getContext()
					.getAuthentication();
			Employee user = (Employee) auth.getPrincipal();
			resp.getWriter().append("{\"name\":'");
			resp.getWriter().append(user.getRealname());
			resp.getWriter().append("'}");
		}
		resp.flushBuffer();
	}

	public void saveNote(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		Employee user = (Employee) auth.getPrincipal();
		Note note = new Note();
		note.setEmployee_id(user.getId());
		note.setCourse_id(NumberUtils.createLong(req.getParameter("course")));
		note.setContent(req.getParameter("content"));
		try {
			NoteService.saveNote(note);
			resp.getWriter().append("{\"success\":true}");
		} catch (Exception e) {
			log.error("", e);
			resp.getWriter().append("{\"success\":false}");
		}

	}

	public void saveNoteFinal(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

	}

	public void addCourse(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		Employee user = (Employee) auth.getPrincipal();
		String cids = req.getParameter("course");
		List<Long> cid = new ArrayList<>();
		for (String s : cids.split(",")) {
			cid.add(NumberUtils.createLong(s));
		}
		try {
			CourseAssignService.addEmployeeCourseAssign(cid, user.getId());
			resp.getWriter().append("{\"success\":true}");
		} catch (Exception e) {
			log.error("", e);
			resp.getWriter().append("{\"success\":false}");
		}
	}

	public void removeCourse(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		Employee user = (Employee) auth.getPrincipal();
		Long cid = NumberUtils.toLong(req.getParameter("course"));
		try {
			CourseAssignService.delEmployeeCourseAssign(cid, user.getId());
			resp.getWriter().append("{\"success\":true}");
		} catch (Exception e) {
			log.error("", e);
			resp.getWriter().append("{\"success\":false}");
		}
	}
}
